package com.hand.hap.demo.service.impl;


import com.hand.hap.core.IRequest;
import com.hand.hap.demo.dto.InvInventoryItems;
import com.hand.hap.demo.dto.OmOrderHeaders;
import com.hand.hap.demo.mapper.OmOrderLinesMapper;
import com.hand.hap.demo.service.IInvInventoryItemsService;
import com.hand.hap.demo.service.IOmOrderHeadersService;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hand.hap.demo.dto.OmOrderLines;
import com.hand.hap.demo.service.IOmOrderLinesService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单行serviceImpl
 * @author shuai.wang02@hand-china.com
 * @date  2018/8/25 19:51
 */

@Service
public class OmOrderLinesServiceImpl extends BaseServiceImpl<OmOrderLines> implements IOmOrderLinesService{

    @Autowired
    private OmOrderLinesMapper orderLinesMapper;

    @Autowired
    private  IOmOrderLinesService orderLinesService;
    @Autowired
    private IOmOrderHeadersService orderHeadersService;
    @Autowired
    private IInvInventoryItemsService invInventoryItemsService;



    @Override
    public List<OmOrderLines> selectByHeaderId(Long headerId) {
        return orderLinesMapper.selectByHeaderId(headerId);
    }



    @Override
    public List<OmOrderLines> ourSelect(IRequest requestContext, OmOrderLines dto, int page, int pageSize) {

        List<OmOrderLines> orderLinesList = new ArrayList<>();
        if (dto!=null){
           orderLinesList = orderLinesMapper.select(dto);

            for (int i = 0; i <orderLinesList.size() ; i++) {
                InvInventoryItems invInventoryItems = new InvInventoryItems();
                invInventoryItems.setInventoryItemId(orderLinesList.get(i).getInventoryItemId());
                invInventoryItems = invInventoryItemsService.selectByPrimaryKey(requestContext, invInventoryItems );
                orderLinesList.get(i).setItemDescription(invInventoryItems.getItemDescription());
                orderLinesList.get(i).setItemCode(invInventoryItems.getItemCode());
                orderLinesList.get(i).setOrderQuantityUom(invInventoryItems.getItemUom());
            }
        }

        return orderLinesList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmOrderLines> submitLines(IRequest request, List<OmOrderLines> list) {
        if (list!=null&&!list.isEmpty()){
            for (int i = 0;i<list.size();i++){
                OmOrderLines orderLines = list.get(i);
                Long lineId =  orderLines.getLineId();
                if (lineId==null){

                    orderLinesService.insertSelective(request,orderLines);
                }else{

                    orderLinesService.updateByPrimaryKeySelective(request,orderLines);
                }
            }
        }

        return list;
    }


    @Override
    public Long findMaxLineNumber(IRequest requestContext, Long headerId) {
          return orderLinesMapper.findMaxLineNumber(requestContext,headerId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteAllSingle(IRequest requestContext, Long headerId) {

         Integer i =0;
        Integer linesNum = countLinesByHeaderId(headerId);
        Integer delLinesNum = orderLinesService.deleteByHeaderId(headerId);
        if (linesNum.equals(delLinesNum)){
            OmOrderHeaders orderHeaders = new OmOrderHeaders();
            orderHeaders.setHeaderId(headerId);
            i = orderHeadersService.deleteByPrimaryKey(orderHeaders);
        }
        return i;
    }

    private Integer countLinesByHeaderId(Long headerId) {
        return orderLinesMapper.countLinesByHeaderId(headerId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteByHeaderId(Long headerId) {
        return orderLinesMapper.deleteByHeaderId(headerId);
    }


}